package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.common.utils.Utility;
import cn.sanshuilin.kst.system.provider.bean.Member;
import cn.sanshuilin.kst.system.provider.bean.Share;
import cn.sanshuilin.kst.system.provider.core.CurrentInfo;
import cn.sanshuilin.kst.system.provider.serviceimpl.CDict;
import com.alibaba.druid.util.StringUtils;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-11-29 10:04
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class ShareReq extends AbstractJpaDao<Share> {

    public List<Share> getSharesByShareRootID(int nPageIndex, int nPageSize,int nStatusID,String shareRootID) {
        try{
            String hql = "SELECT s FROM Share s WHERE s.StatusID = :StatusID AND s.ShareRootID = :ShareRootID ";
            Query query = entityManager.createNativeQuery(hql,Share.class);
            query.setParameter("StatusID", nStatusID);
            query.setParameter("ShareRootID", shareRootID);

            if(nPageIndex!=-1 && nPageSize!=-1){
                query.setFirstResult(nPageIndex * nPageSize);
                query.setMaxResults(nPageSize);
            }
            List<Share> list = query.getResultList();
            return list;
        }catch (Exception e) {
            LogPrinter.info("ssss" + e.getMessage());
            return null;
        }

    }

    public List<Share> getViewShares(int nPageIndex,int nPageSize,String shareParentID,String shareRootID) {
        List<Share> list=new ArrayList<Share>();
        try{
            String hql = "SELECT s FROM Share s WHERE 1=1 ";

            if(!"".equals(shareParentID)){
                hql +="AND s.ShareParentID= :ShareParentID";
            }
            if(!"".equals(shareRootID)){
                hql +="AND s.ShareRootID= :ShareRootID";
            }


            hql += " ORDER BY s.PubDate DESC ";
            Query query = entityManager.createNativeQuery(hql,Share.class);
            if(!"".equals(shareParentID)){
                query.setParameter("ShareParentID", shareParentID);
            }
            if(!"".equals(shareRootID)){
                query.setParameter("ShareRootID", shareRootID);
            }
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            list = query.getResultList();

        }
        catch (Exception e) {
            LogPrinter.info("getViewShares12221111111111" + e.getMessage());
        }
        return list;
    }




    public long getSharesCount(String strKeyword,int searchCategoryID,int backstage,String searchPubMemberID,int typeID,String searchTime,int hotID,int searchCommentCount,int isFocusID,int nStatusID,String locationID,int heat,String searchFromDate,String searchToDate) {
        long count = 0;
        try {
            Query query = this.getSharesQuery(" COUNT(*) ", strKeyword,searchCategoryID,backstage,searchPubMemberID,typeID,searchTime,hotID,searchCommentCount,isFocusID, nStatusID,locationID,heat,searchFromDate,searchToDate);
            count = Utility.toSafeLong(query.getSingleResult());
        } catch (Exception e) {
            LogPrinter.error("getSharesCount" + e.getMessage());
        }
        return count;
    }


    public List<Share> getShareImgBySubject(String strSubject) {
        String hql = "SELECT s FROM Share s WHERE s.content LIKE :Subject AND s.imageIds <> null";
        Query query = entityManager.createQuery(hql);
        query.setParameter("Subject", strSubject);
        List<Share> shares = query.getResultList();
        return shares;
    }



    public List<Share> getNewShares(String strShareID,int searchCategoryID,int hotID ){
        List<Share> list = new ArrayList<Share>();
        String hql = " SELECT s FROM Share s WHERE s.PubDate > (SELECT sh.PubDate FROM Share sh WHERE sh.ID =:ShareID)  ";
        if (hotID>0) {
            hql+=" AND s.HotID= :HotID";
        }
        if (searchCategoryID>0) {
            hql+=" AND s.CategoryID= :searchCategoryID";
        }
        hql += " ORDER BY s.PubDate DESC ";
        Query query = entityManager.createNativeQuery(hql,Share.class);
        query.setParameter("ShareID", strShareID);
        if (hotID>0) {
            query.setParameter("HotID",hotID);
        }
        if (searchCategoryID>0) {
            query.setParameter("searchCategoryID",searchCategoryID);
        }
        list=query.getResultList();

        return list;

    }


    public List<Share> getMyShares(int nPageIndex, int nPageSize, String strKeyword,
                                  int searchCategoryID, int searchTypeID,
                                  int hotID, int focusID, int nStatusID, String locationID,
                                  int heat, String searchFromDate, String searchToDate) {
        List<Share> list = new ArrayList<Share>();
            if (StringUtils.isEmpty(searchFromDate)) {
                searchFromDate = "1990-01-01";
            }
            if (StringUtils.isEmpty(searchToDate)) {
                searchToDate = "2090-01-01";
            }
            Member member = CurrentInfo.getCurrentMember();
            String hql = "SELECT s FROM Share s WHERE 1=1";

            if (searchCategoryID!= -1) {
                hql+=" AND s.CategoryID= :searchCategoryID ";
            }
            if(searchCategoryID== CDict.Information.getId()){
                if(strKeyword!=""){
                    hql+=" AND (s.Title LIKE :Keyword ) ";
                }
            }
            if(searchCategoryID==CDict.Share.getId()){
                if(strKeyword!=""){
                    hql+=" AND (s.Content LIKE :Keyword ) ";
                }
            }
            if (hotID!= -1) {
                hql+=" AND s.HotID= :HotID";
            }
            if (searchTypeID!= -1) {
                hql+=" AND s.TypeID= :searchTypeID";
            }
            if (focusID!= -1) {
                hql+=" AND s.IsFocusID= :focusID";
            }
            if (nStatusID!= -1) {
                hql+=" AND s.StatusID= :StatusID";
            }
            if(!"".equals(locationID)){
                LogPrinter.info("11111111111locationIDlocationIDlocationID");
                hql +=" AND (s.LocationID= :locationID OR s.LocationID IS NULL)";
            }
            hql += " AND s.PubDate>= :FromDate AND s.PubDate< :ToDate  ";

            if(heat!=-1){
                hql+=" ORDER BY (s.RelayCount+s.CommentCount+s.AttitudeCount+s.AttentionCount) DESC";
            }else{
                hql += " ORDER BY s.IsTopID DESC,s.PubDate DESC ";
            }
            Query query = entityManager.createNativeQuery(hql,Share.class);
            if (searchCategoryID!= -1) {
                query.setParameter("searchCategoryID", searchCategoryID);
            }
            if(searchCategoryID==CDict.Information.getId()){
                if(strKeyword!=""){
                    query.setParameter("Keyword", "%" + strKeyword + "%");
                }
            }
            if(searchCategoryID==CDict.Share.getId()){
                if(strKeyword!=""){
                    query.setParameter("Keyword", "%" + strKeyword + "%");
                }
            }
            if (hotID!= -1) {
                query.setParameter("HotID", hotID);
            }
            if (searchTypeID!= -1) {
                query.setParameter("searchTypeID", searchTypeID);
            }
            if (focusID!= -1) {
                query.setParameter("focusID", focusID);
            }
            if (nStatusID!= -1) {
                query.setParameter("StatusID", nStatusID);
            }
            if(!"".equals(locationID)){
                query.setParameter("locationID", locationID);
            }
            query.setParameter("FromDate",Utility.toSafeDateTime(searchFromDate));
            query.setParameter("ToDate", DateUtils.addDays(Utility.toSafeDateTime(searchToDate), 1));

            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            list = query.getResultList();

            return list;
    }
    public List<Share> getShares(int nPageIndex, int nPageSize, String strKeyword,int searchCategoryID,
                                 int backstage,String searchPubMemberID,int searchTypeID,
                                 String searchTime,int hotID,int commentCount,int focusID,
                                 int nStatusID,String locationID,int heat,String searchFromDate,
                                 String searchToDate) {
        List<Share> list = new ArrayList<Share>();
        try {
            Query query = getSharesQuery("s", strKeyword,searchCategoryID,backstage,searchPubMemberID,searchTypeID,
                    searchTime,hotID,commentCount,focusID,
                    nStatusID,locationID,heat,searchFromDate,searchToDate);
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            list = query.getResultList();

        } catch (Exception e) {
            LogPrinter.error("getShares" + e.getMessage());
        }
        return list;
    }
    private Query getSharesQuery(String strChange, String strKeyword, int
            searchCategoryID, int backstage, String searchPubMemberID,
                                 int searchTypeID, String searchTime, int hotID,
                                 int commentCount, int focusID, int nStatusID, String locationID,
                                 int heat, String searchFromDate, String searchToDate
                                 ) {

        if (StringUtils.isEmpty(searchFromDate)) {
            searchFromDate = "1990-01-01";
        }
        if (StringUtils.isEmpty(searchToDate)) {
            searchToDate = "2090-01-01";
        }
        Member member = CurrentInfo.getCurrentMember();
        String hql = "SELECT " + strChange + " FROM Share s WHERE 1=1";
        if(!"".equals(searchPubMemberID)){
            hql +=" AND s.PubMemberID= :searchPubMemberID";
        }
        if (searchCategoryID!= -1) {
            hql+=" AND s.CategoryID= :searchCategoryID ";
        }
        if(searchCategoryID==CDict.Information.getId()){
            if(strKeyword!=""){
                hql+=" AND (s.Title LIKE :Keyword ) ";
            }
        }
        if(searchCategoryID== CDict.Share.getId()){
            if(strKeyword!=""){
                hql+=" AND (s.Content LIKE :Keyword ) ";
            }
        }
        if (hotID!= -1) {
            hql+=" AND s.HotID= :HotID";
        }
        if (commentCount!= -1) {
            hql+=" AND s.CommentCount > :CommentCount";
        }
        if (backstage!= -1) {
            if(!member.getGroupId().equals(CDict.MemberGroupAdmin.getId())&&
                    !member.getGroupId().equals(CDict.MemberCheck.getId())){
                hql+=" AND s.PubMemberID= :PubMemberID";
            }
        }
        if (searchTypeID!= -1) {
            hql+=" AND s.TypeID= :searchTypeID";
        }
        if (focusID!= -1) {
            hql+=" AND s.IsFocusID= :focusID";
        }
        if (nStatusID!= -1) {
            hql+=" AND s.StatusID= :StatusID";
        }
        if(!"".equals(locationID)){
            LogPrinter.info("11111111111locationIDlocationIDlocationID");
            hql +=" AND (s.LocationID= :locationID OR s.LocationID IS NULL)";
        }
        hql += " AND s.PubDate>= :FromDate AND s.PubDate< :ToDate  ";
        if(!"".equals(searchTime)){
            if("day".equals(searchTime)){
                LogPrinter.info("11111111111111");
                hql+=" AND DATE_FORMAT(PubDate,'%Y-%m-%d') =DATE_FORMAT(now(),'%Y-%m-%d')";
            }
            if("week".equals(searchTime)){
                hql+=" AND YEARWEEK(DATE_FORMAT(PubDate,'%Y-%m-%d')) =YEARWEEK(now())";
            }
            if("mouth".equals(searchTime)){
                hql+=" AND DATE_FORMAT(PubDate,'%Y-%m')=DATE_FORMAT(now(),'%Y-%m')";
            }
            hql+="ORDER BY (s.RelayCount+s.CommentCount) DESC";
        }
        else{
            if(heat!=-1){
                hql+=" ORDER BY (s.RelayCount+s.CommentCount+s.AttitudeCount+s.AttentionCount) DESC";
            }else{
                hql += " ORDER BY s.IsTopID DESC,s.PubDate DESC ";
            }

        }
        Query query = entityManager.createNativeQuery(hql);
        //LogPrinter.info(query)

        if (searchCategoryID!= -1) {
            query.setParameter("searchCategoryID", searchCategoryID);

        }
        if (commentCount!= -1) {
            query.setParameter("CommentCount", commentCount);
        }
        if(searchCategoryID==CDict.Information.getId()){
            if(strKeyword!=""){
                query.setParameter("Keyword", "%" + strKeyword + "%");
            }
        }
        if(searchCategoryID==CDict.Share.getId()){
            if(strKeyword!=""){
                query.setParameter("Keyword", "%" + strKeyword + "%");
            }
        }
        if (backstage!= -1) {
            if(!member.getGroupId().equals(CDict.MemberGroupAdmin.getId())&& !member.getGroupId().equals(CDict.MemberCheck.getId())){
                query.setParameter("PubMemberID", member.getId());
            }
        }
        if (hotID!= -1) {
            query.setParameter("HotID", hotID);
        }
        if(!"".equals(searchPubMemberID)){
            query.setParameter("searchPubMemberID", searchPubMemberID);
        }
        if (searchTypeID!= -1) {
            query.setParameter("searchTypeID", searchTypeID);
        }
        if (focusID!= -1) {
            query.setParameter("focusID", focusID);
        }
        if (nStatusID!= -1) {
            query.setParameter("StatusID", nStatusID);
        }
        if(!"".equals(locationID)){
            query.setParameter("locationID", locationID);
        }
        query.setParameter("FromDate", Utility.toSafeDateTime(searchFromDate));
        query.setParameter("ToDate", DateUtils.addDays(Utility.toSafeDateTime(searchToDate), 1));
        LogPrinter.info("------------------------------------------"+query+"-----------------------------------------");

        return query;
    }
}
